package com.hframe.utils;

public class Arrays {

	public static int binarySearch(byte[] buffer, byte[] jpg) {
		return binarySearch(buffer, 0, buffer.length, jpg);
	}

	public static int binarySearch(byte[] buffer, int fromIndex, int toIndex, byte[] jpg) {
		toIndex += jpg.length;
		if (fromIndex >= 0 && toIndex > fromIndex) {
			if (toIndex > buffer.length)
				toIndex = buffer.length;

			for (int i = fromIndex; i < toIndex - jpg.length; i++) {
				if (searchT(buffer, i, jpg))
					return i;
			}
		}
		return -1;
	}

	private static <T> boolean searchT(byte[] buffer, int fromIndex, byte[] jpg) {
		for (int i = 0; i < jpg.length; i++) {
			if (buffer[fromIndex + i] != jpg[i])
				return false;
		}
		return true;
	}

}